﻿using System;
using System.Threading;
using System.Web.UI;
using visinia.SmartEngine;
using visinia.SmartEngine.Site;

namespace visinia.web.admin
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AddScriptVariables();
            WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/jquery-1.3.2.min.js");
            WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.core.js");
            WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "jquery/ui.accordion.js");
            WebPageController.AddScriptLink(Page, WebRoots.GetAdminScriptsRoot + "Admin/jconfirmaction.jquery.js");
            // for easy and light weight data transfer
            WebPageController.AddScriptLink(Page, WebRoots.GetJsRoot + "json/jquery.json-2.2.min.js");
            WebPageController.AddScriptLink(Page, WebRoots.GetAdminScriptsRoot + "Admin/admin.js");
            WebPageController.AddAdminThemeAwareCssLink(Page, "admin-form.css");
            Header.Controls.Add(new LiteralControl("<link rel=\"shortcut icon\" type=image/x-icon href=\"" + WebRoots.GetWebRoot + "favicon.ico\" />"));

            if (!(Request.QueryString["page"] == null))
            {
                SystemMenuHeader.Value = Request.QueryString["header"];
                SystemMenuPage.Value = Request.QueryString["page"];
                if (SystemMenuHeader.Value.StartsWith("_"))
                {
                    SystemMenuHeader.Value = SystemMenuHeader.Value.Remove(0, 1);
                    Bind(Request.QueryString["header"], SystemMenuPage.Value, true);
                }
                else
                    Bind(Request.QueryString["header"], SystemMenuPage.Value, false);
            }
            else
            {   //show the default page when entring the admin mode
                SystemMenuHeader.Value = "Admin";
                SystemMenuPage.Value = "Settings";
                Bind("_Admin", SystemMenuPage.Value, true);
            }
        }

        void Page_PreInit(Object sender, EventArgs e)
        {
            MasterPageFile = WebRoots.GetAdminRoot + "Themes/" + SiteSettings.AdminTheme + "/admin.master";
        }

        private void AddScriptVariables()
        {
            string root = "<input type=\"hidden\" value=\"" + WebRoots.GetWebRoot + "\" id=\"WebRoots_GetWebRoot\" />";
            LiteralControl rootctrl = new LiteralControl(root);
            Page.Form.Controls.Add(rootctrl);
        }

        #region load control
        private void Bind(string header, string page, bool SystemMenuPage)
        {
            SystemMenuHeader pages = null;
            if (SystemMenuPage)
                pages = SystemMenu.GetSystemMenu().Find(p => string.Compare(p.Header, header, true) == 0);
            else
                pages = SystemMenu.GetPluginMenu().Find(p => string.Compare(p.Header, header, true) == 0);
            if (pages != null)
            {
                SystemMenuPage requestedPage = pages.Page.Find(p => string.Compare(p.Name, page, true) == 0);
                if (requestedPage == null)
                    return;
                if (string.IsNullOrEmpty(pages.Header))
                    return;
                if (string.Compare(page, requestedPage.Name) != 0)
                    return;
                bool global = false;
                if (string.IsNullOrEmpty(requestedPage.Role))
                    global = true;
                if (Thread.CurrentPrincipal.IsInRole(requestedPage.Role) || (Thread.CurrentPrincipal.IsInRole("admin")) || (global))
                {
                    string control = requestedPage.Control;
                    string root = WebRoots.GetAdminRoot + "Pages\\";
                    Control usercontrol = LoadControl(root + control);
                    Container.Controls.Add(usercontrol);
                    //// load help file                                 
                    //if (!string.IsNullOrEmpty(requestedPage.Help))
                    //    Helpbar.HelpFile = requestedPage.Help;

                    Title = "visinia admin mode | " + header + " - " + Translate(requestedPage.FriendlyName);
                }
            }
        }
        #endregion
        
        private string Translate(string txt)
        {
            object translation = GetGlobalResourceObject("translation", txt);
            if (translation != null)
                return translation.ToString();
            return string.Format("[{0}]", txt);
        }
    }
}